package com.study.d14_tcp_bs_plus;

import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class Server {
    public static void main(String[] args) throws Exception{
        System.out.println("=====服务端启动！=====");
        //1、创建ServerSocket对象，同时为服务端注册端口
        ServerSocket ss = new ServerSocket(8888);
        //创建出一个线程池，负责处理通信管道的业务
        ThreadPoolExecutor pool = new ThreadPoolExecutor(16, 16, 0, TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(8), Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());


        while (true) {
            //2、使用ServerSocket对象，调用一个accept方法，等待客户端的连接请求
            Socket socket = ss.accept();
            System.out.println(socket.getRemoteSocketAddress()+"上线了");
            //3、把这个客户端对应的socket通信管道，交给一个独立的线程负责处理
            pool.execute(new ServerReaderRunnable(socket));

        }


    }
}
